using System;
using System.Data;
using System.Data.SqlClient;
using SMS.SMPPClient.PDU.Incoming;
using WorkThreading;
using SMS.BLL;

namespace SMS.SMPPClient.Common
{
    public class MsgInWorkItem : WorkItem
    {
        // Fields
        private string _messageInfo;
        private int _requestID;
        private string _serviceID;
        private string _userID;
        private Gateway frmMain;

        // Methods
        public MsgInWorkItem(SMPPDeliverSM MsgIn, Gateway fr)
        {
            this.frmMain = null;
            this._userID = MsgIn.SourceAddress;
            this._serviceID = MsgIn.DestAddress;
            this._messageInfo = MsgIn.ShortMessage.ToString();
            this._requestID = MsgIn.SequenceNumber;
            this.frmMain = fr;
        }

        public MsgInWorkItem(string UserID, string ServiceID, string MessageInfo, int RequestID, Gateway fr)
        {
            this._userID = UserID;
            this._serviceID = ServiceID;
            this._messageInfo = MessageInfo;
            this._requestID = RequestID;
            this.frmMain = fr;
        }

        private void InsertMsgIn()
        {
            if (this._messageInfo.Trim() != "")
            {
                string connStr = ConfigApp.ConnStr;
                
                if (this._serviceID.Length > 3)
                    this._serviceID = this._serviceID.Substring(this.ServiceID.Length - 4);
                
                if (this._serviceID != "6198" && this._serviceID != "6298" && this._serviceID != "6398" && this._serviceID != "6498" && this._serviceID != "6598" && this._serviceID != "6698" && this._serviceID != "6798")
                    throw new Exception("RequestID " + this._requestID.ToString() + " Invalid ServiceID = " + this._serviceID);

                string _servid = "";
                if (this._serviceID == "6198") _servid = "81";
                else if (this._serviceID == "6298") _servid = "82";
                else if (this._serviceID == "6398") _servid = "83";
                else if (this._serviceID == "6498") _servid = "84";
                else if (this._serviceID == "6598") _servid = "85";
                else if (this._serviceID == "6698") _servid = "86";
                else if (this._serviceID == "6798") _servid = "87";

                string cmdText = ("INSERT INTO MsgIn_" + _servid + "(UserID,ServiceID,Message,RequestID)") + " VALUES (@UserID,@ServiceID,@Message,@RequestID)";
                
                SqlConnection cnn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand(cmdText, cnn);
                cmd.Parameters.Clear();
                
                SqlParameter param1 = new SqlParameter("@UserID", SqlDbType.VarChar, 20);
                param1.Value = this._userID;
                cmd.Parameters.Add(param1);
                
                SqlParameter param2 = new SqlParameter("@ServiceID", SqlDbType.VarChar, 20);
                param2.Value = this._serviceID;
                cmd.Parameters.Add(param2);
                
                SqlParameter param3 = new SqlParameter("@Message", SqlDbType.NVarChar, 160);
                param3.Value = this._messageInfo;
                cmd.Parameters.Add(param3);
                
                SqlParameter param4 = new SqlParameter("@RequestID", SqlDbType.VarChar, 50);
                param4.Value = this._requestID;
                cmd.Parameters.Add(param4);
                
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
        }

        public override void Perform()
        {
            try
            {
                lock (this.frmMain)
                {
                    this.frmGateway.SmppClient.DeliverSmRes(new SmppDeliverSmRes(this._requestID, ""));
                }
                this.InsertMsgIn();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        // Properties
        public string MessageInfo
        {
            get { return this._messageInfo; }
            set { this._messageInfo = value; }
        }

        public int RequestID
        {
            get { return this._requestID; }
            set { this._requestID = value; }
        }

        public string ServiceID
        {
            get { return this._serviceID; }
            set { this._serviceID = value; }
        }

        public string UserID
        {
            get { return this._userID; }
            set { this._userID = value; }
        }
    }
}

